/**
 *  Copyright (c) 2014 The xmlepf project.  All rights reserved.
 *
 *  This file is part of xmlepf (Event Driven Testing Framework).
 *
 *  xmlepf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  xmlepf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with xmlepf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/xmlepf.
 */
package org.xmlepf.xtemp.event;

import java.util.LinkedList;
import java.util.List;
import net.sf.saxon.Configuration;
import org.xmlepf.utils.ObjectCreator;
import org.xmlepf.xml.adapter.NodeSerializer;
import org.xmlepf.xml.adapter.XMLNode;
import org.xmlepf.xtemp.eventboard.EventBoard;
import org.xmlepf.xtemp.eventboard.EventBoardManager;

/**
 *
 *
 */
public class EventManager {

    private EventBoardManager boardManager;

    public EventManager() {
    }

    public void setEventBoardManager( EventBoardManager boardManager) {
        this.boardManager = boardManager;
    }

    public void postEvent(Event event) throws Exception {
        for (EventBoard board : boardManager.getEventBoards()) {
            if (board.isMatchingEvent(event)) {
                board.postEvent(event);
            }
        }
    }
    
    public void postEvents(List<Event> eventsList) throws Exception {
        for (EventBoard board : boardManager.getEventBoards()) {
            for (Event event : eventsList) {
                if (board.isMatchingEvent(event)) {
                    board.postEvent(event);
                }
            }
        }
    }

    public Event catchEvent(String boardName, String filter, long maxTimeout) throws Exception {
        if (boardManager.hasEventBoard(boardName)) {
            EventBoard board = boardManager.getEventBoard(boardName);
            return board.catchEvent(filter, maxTimeout);
        }
        throw new Exception("Cannot start catching events, because event board '"
                + boardName + "' is not known. Filter used '" + filter + "'.");
    }
    
    public List<Event> catchEvents(String boardName, String filter, long maxTimeout) throws Exception {
        if (boardManager.hasEventBoard(boardName)) {
            EventBoard board = boardManager.getEventBoard(boardName);
            return board.catchEvents(filter, maxTimeout);
        }
        throw new Exception("Cannot start catching events, because event board '"
                + boardName + "' is not known. Filter used '" + filter + "'.");
    }

    public String serialize(List<Event> matchedEvents) {
        StringBuilder serializedEvents = new StringBuilder(2000); // todo: analyze this value

        for (Event event : matchedEvents) {
            serializedEvents.append(event.getContent());
        }

        if (serializedEvents.length() == 0) {
            serializedEvents.append("<event/>");
        }

        return serializedEvents.toString();
    }
    
    public XMLNode mergeEvents(List<Event> matchedEvents, Configuration xPathConfig) throws Exception {
        List<XMLNode> nodes = new LinkedList<XMLNode>();
        
        for (Event event : matchedEvents) {
            nodes.add( event.getNode() );
        }

        NodeSerializer s = (NodeSerializer) ObjectCreator.get("NodeSerializer");
        return s.mergeNodes(nodes, xPathConfig);
    }
}
